package com.twp.imtest;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

public class MainActivity extends AppCompatActivity {

    private ViewPager mVp;
    private List<Fragment> mFlist;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userId = getIntent().getStringExtra("userid");
        getSupportActionBar().setTitle("userId="+userId);
        mVp = findViewById(R.id.main_vp);
        mFlist = new ArrayList<>();
        mFlist.add(getConListFragment());
        mFlist.add(FragmentPageTest.getInstance(userId.equals("123") ? "124" : "123"));
        mFlist.add(FragmentPageMe.getInstance());
        mFlist.add(FragmentPageTruly.getInstance());
        MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager());
        mVp.setAdapter(adapter);
    }

    private ConversationListFragment getConListFragment() {
        ConversationListFragment fragment = new ConversationListFragment();
        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话，该会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//设置群组会话，该会话非聚合显示
                .build();
        fragment.setUri(uri);  //设置 ConverssationListFragment 的显示属性
        return fragment;
    }


    private class MyPageAdapter extends FragmentStatePagerAdapter {

        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFlist.get(position);
        }

        @Override
        public int getCount() {
            return mFlist.size();
        }
    }
}
